﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HafriyatTest
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }



        public DataTable Fill(string sqlText)
        {
            DataTable dt = new DataTable();
            using (SqlConnection cn = new SqlConnection(@"Data Source=AKARSRV\mikro;Database=MikroDB_V15_AKARYILDIZ;user id=bilisim;password=123"))
            {
                try
                {
                    using (SqlCommand cmd = new SqlCommand(sqlText, cn))
                    {
                        using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                        {
                            adp.Fill(dt);
                            cmd.Parameters.Clear();
                        }
                    }
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally { if (cn.State == ConnectionState.Open) cn.Close(); }
            }
            return dt;
        }

        public void StartExport(int id)
        {
            HFService.ServiceSoapClient service = new HFService.ServiceSoapClient();

            DataTable dt = Fill("select * from HAFRIYAT_HAREKETLERI where ID=" + id.ToString());
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Fis fis = new Fis();
                fis.AracTipi = dt.Rows[i]["AracTipi"].ToString();
                fis.TerminalNo = dt.Rows[i]["TerminalNo"].ToString();
                fis.IslemCinsi = Cevir.Parse<byte>(dt.Rows[i]["IslemTipi"]);
                fis.Tarih = Cevir.Parse<DateTime>(dt.Rows[i]["Tarih"]);
                fis.DokumYeri = dt.Rows[i]["DokumYeri"].ToString();
                fis.Plaka = dt.Rows[i]["Plaka"].ToString();
                fis.Santiye = dt.Rows[i]["Santiye"].ToString();
                fis.Fiyat = Cevir.Parse<decimal>(dt.Rows[i]["Fiyat"]);
                fis.AracTipi = dt.Rows[i]["AracTipi"].ToString();
                fis.MalzemeTipi = dt.Rows[i]["MalzemeTipi"].ToString();
                fis.Litre = Cevir.Parse<decimal>(dt.Rows[i]["Litre"]);
                fis.UniqueID = dt.Rows[i]["UniqueID"].ToString();
                fis.Sira = dt.Rows[i]["MikroSira"].ToString();

                string sonuc = service.AddRecord(JsonConvert.SerializeObject(fis));

                if (sonuc == "OK")
                {
                   // MessageBox.Show("aktardı");
                }
                else
                {
                    MessageBox.Show(sonuc);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = Fill("select * from HAFRIYAT_HAREKETLERI where send=0");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(dataGridView1.Rows.Count.ToString());

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                //MessageBox.Show(Convert.ToInt32(dataGridView1["ID", i].Value).ToString());

                StartExport(Convert.ToInt32(dataGridView1["ID", i].Value));
                this.Text = i.ToString();
            }

            MessageBox.Show("bitti");
        }
    }
}
